package org.zhangyp.n01jdkapi.n04thread;
/**
 * 哲学家
 * @author ypzhang
 * @since 2025/8/8
 */
public class Philosopher implements Runnable {
    private final Chopstick left;
    private final Chopstick right;
    private final String name;
    public Philosopher(String name, Chopstick left, Chopstick right) {
        this.name = name;
        this.left = left;
        this.right = right;
    }

    @Override
    public void run() {
        System.out.println(name + " 开始思考");
        synchronized (left){
            left.take(this.name);
            synchronized (right){
                right.take(this.name);
                System.out.println(name + " 开始吃饭");
                right.down(this.name);
            }
            left.down(this.name);
        }
        System.out.println(name + " 吃完饭了");
    }
}
